//
//  TZYSoundCell.m
//  Moment
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "TZYSoundCell.h"

@interface TZYSoundCell ()

@property(nonatomic, strong) UILabel *type;/**<Sound 类型*/   //1
@property(nonatomic, strong) UILabel *titleLabel;/**<Sound 标题*/   //4
@property(nonatomic, strong) UILabel *author;/**<Sound 发布者*/   //5
@property(nonatomic, strong) UIImageView *soundImageView;/**<Sound 图片*/ //2
@property(nonatomic, strong) UIImageView *playImageView;/**<Sound 播放图片*/ //3
@property(nonatomic, strong) UILabel *zanLabel;/**<Sound 赞*/
@property(nonatomic, strong) UIButton *heartButton;/**<Sound 赞 心型*/

@end


@implementation TZYSoundCell



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

#pragma mark - 子视图
- (void)createSubView
{

    self.type = [[UILabel alloc] init];
    //self.type.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:self.type];
    
    [self.type mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(@10);
        make.width.mas_equalTo(@150);
        make.height.mas_equalTo(@20);
        
    }];
    
    
    
    
    self.soundImageView = [[UIImageView alloc] init];
    //self.soundImageView.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:self.soundImageView];
    
    [self.soundImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@(SCREEN_WIDTH / 5));
        make.right.mas_equalTo(@(-SCREEN_WIDTH / 5));
        make.top.mas_equalTo(self.type.mas_bottom).offset(10);
        make.height.mas_equalTo(@(SCREEN_WIDTH * (2.0 / 5)));
        
    }];
    
    
    
    
    
    self.titleLabel = [[UILabel alloc] init];
    //self.titleLabel.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.titleLabel];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.soundImageView);
        make.top.mas_equalTo(self.soundImageView.mas_bottom).offset(20);
        make.height.mas_equalTo(@(SCREEN_WIDTH * (2.0 / 12)));
        
    }];
    
    
    
    
    
    self.author = [[UILabel alloc] init];
    //self.author.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:self.author];
    
    [self.author mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.soundImageView);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(20);
        make.height.mas_equalTo(@20);
        
    }];
    
    
    
    
    
    self.playImageView = [[UIImageView alloc] init];
    //self.playImageView.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:self.playImageView];
    self.playImageView.image = [UIImage imageNamed:@"ic_ting_play.png"];
    [self.playImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.soundImageView.mas_bottom);
        make.width.mas_equalTo(@(SCREEN_WIDTH * (2.0 / 14)));
        make.height.mas_equalTo(@(SCREEN_WIDTH * (2.0 / 14)));
        
        
    }];
    
    //裁掉边框
    self.playImageView.backgroundColor = [UIColor colorWithRed:0.74 green:0.74 blue:0.74 alpha:1];
    self.playImageView.layer.borderColor = [UIColor colorWithRed:0.74 green:0.74 blue:0.74 alpha:1].CGColor;
    self.playImageView.layer.masksToBounds = YES;
    //设置半径
    self.playImageView.layer.cornerRadius = SCREEN_WIDTH * (2.0 / 28);
    //边框宽度
    self.playImageView.layer.borderWidth = 0;
    
    
//    //钮图片
//    UIImageView *imgView = [[UIImageView alloc]init];
//    imgView.image = [UIImage imageNamed:@"iconfont-play-2.png"];
//    imgView.layer.masksToBounds = NO;
//    imgView.layer.cornerRadius = SCREEN_WIDTH * (2.0 / 24);
//    [self.playImageView addSubview:imgView];
//    
//    [imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.center.mas_equalTo(self.soundImageView.mas_bottom);
//        make.width.mas_equalTo(@(SCREEN_WIDTH * (2.0 / 12)));
//        make.height.mas_equalTo(@(SCREEN_WIDTH * (2.0 / 12)));
//        
//    }];
//    
    
    
    
    
    
    self.zanLabel = [[UILabel alloc] init];
    //self.zanLabel.backgroundColor = [UIColor purpleColor];
    [self.contentView addSubview:self.zanLabel];
    
    [self.zanLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@(-20));
        make.width.mas_equalTo(@60);
        make.top.equalTo(self.author.mas_bottom).offset(20);
        make.height.equalTo(@20);
        
    }];
    
    
    
    self.heartButton = [[UIButton alloc] init];
    //self.heartButton.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:self.heartButton];
    
    [self.heartButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.zanLabel.mas_left).offset(-10);
        make.width.mas_equalTo(@20);
        make.height.mas_equalTo(@20);
        make.top.equalTo(self.zanLabel);
        
    }];
    
    
    [self.heartButton addTarget:self action:@selector(likeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}


#pragma mark- 喜欢按钮实现方法
- (void)likeBtnAction:(UIButton *)btn{
    
    NSMutableArray *array = [[CollectViewController collectDataBase]selectCollectList];
    
    NSMutableArray *array2 = [NSMutableArray array];
    
    for (TZYHomeData *model in array) {
        [array2 addObject:model._id];
    }
    if ([array2 containsObject: self.homeData._id]) {
        
        TZYHomeData *model = [[TZYHomeData alloc]init];
        model.title = self.homeData.title;
        [[CollectViewController collectDataBase] deleteMyCollectData:model.title];
        
        // 关键帧动画,改变view缩放属性
        CAKeyframeAnimation *keyAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        // 设置关键帧的值,values是数组类型
        keyAnimtion.values = @[@(1.5), @(1.0), @(0.5)];
        keyAnimtion.duration = 0.2;
        [btn.layer addAnimation:keyAnimtion forKey:@"key"];
        
        [self.heartButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] - 1];
        
    }else{
        TZYHomeData *model = [[TZYHomeData alloc]init];
        model._id = self.homeData._id;
        model.title = self.homeData.title;
        model.coverimg = self.homeData.coverimg;
        model.name = self.homeData.name;
        
        [[CollectViewController collectDataBase]inserCollectList:model];
        
        
        // 关键帧动画,改变view缩放属性
        CAKeyframeAnimation *keyAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        // 设置关键帧的值,values是数组类型
        keyAnimtion.values = @[@(0.1), @(1.0), @(1.5)];
        keyAnimtion.duration = 0.2;
        [btn.layer addAnimation:keyAnimtion forKey:@"key"];
        // 2.点击按钮时做判断
        [self.heartButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] + 1];
    }
}




- (void)setHomeData:(TZYHomeData *)homeData
{
    if (_homeData != homeData) {
        _homeData = homeData;
    }
    
    self.type.text = [NSString stringWithFormat:@"%@ · %@",self.homeData.name,self.homeData.enname];
    self.type.font = [UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]];
    
    
    [self.soundImageView sd_setImageWithURL:self.homeData.coverimg placeholderImage:PLACEHOLDER_IMAGE];
    
    
    self.titleLabel.text = homeData.title;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:[TZYFontSize getFontSizeForBig]];
    self.titleLabel.numberOfLines = 0;
    
    self.author.text = [NSString stringWithFormat:@"by:%@",[self.homeData.userinfo objectForKey:@"uname"]];
    self.author.font = [UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]];
    
    
    
    self.zanLabel.text = [NSString stringWithFormat:@"%@",self.homeData.like];
    self.zanLabel.font = [UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]];
    
    
    // 1.上拉下拉cell会重用cell,所以需要在赋值的时候加判断
    [self.heartButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
    if (_homeData.selected) {
        [self.heartButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
    }
    //判断收藏状态
    NSMutableArray *array = [[CollectViewController collectDataBase]selectCollectList];
    NSMutableArray *array2 = [NSMutableArray array];
    for (TZYHomeData *model in array) {
        [array2 addObject:model._id];
    }
    if ([array2 containsObject: self.homeData._id]) {
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
    }
    
    
    
    
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
